package com.szqhyxj.youxiangju.biz.partnerHome.shop.purchase;

import android.text.TextUtils;

import com.szqhyxj.youxiangju.biz.core.UserInfo;
import com.szqhyxj.youxiangju.biz.core.UserInfoManger;
import com.szqhyxj.youxiangju.biz.core.UserInfoModel;
import com.szqhyxj.youxiangju.biz.merchantHome.bean.CommoditeDetails;
import com.szqhyxj.youxiangju.biz.merchantHome.bean.DefaultSite;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.ShopModel;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.site.SiteModel;
import com.szqhyxj.youxiangju.sys.AbBasePresenter;
import com.szqhyxj.youxiangju.sys.BaseResult;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

class PartnerPreparePurchasePresenter extends AbBasePresenter implements PartnerPreparePurchaseContract.Presenter {

    private final PartnerPreparePurchaseContract.View view;
    private CommoditeDetails data;
    private boolean isChecked = false;
    private ShopModel.OrderEntity orderEntity;

    public PartnerPreparePurchasePresenter(PartnerPreparePurchaseContract.View view) {
        this.view = view;
    }


    @Override
    public void init(CommoditeDetails data) {
        view.showCommodite(data);
        orderEntity = new ShopModel.OrderEntity();
        orderEntity.setPackageId(data.getPackageId() + "");
    }

    @Override
    public void initSite() {
        addDisposables(SiteModel.getDefaultAddress().observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<DefaultSite>>() {
                    @Override
                    public void accept(BaseResult<DefaultSite> defaultSiteBaseResult) throws Exception {
                        view.dismissLoading();
                        if (defaultSiteBaseResult.businessLogicSuccess()) {
                            if (defaultSiteBaseResult.getData() != null) {
                                view.showSite(defaultSiteBaseResult.getData());

                                orderEntity.setAddress(defaultSiteBaseResult.getData().getAddrId() + "");
                            }
                        } else {
                            view.showToast(defaultSiteBaseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showToast(throwable.getMessage());
                    }
                }));
    }

    public void initSite(String addrId) {
        addDisposables(SiteModel.detailDddress(addrId).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<DefaultSite>>() {
                    @Override
                    public void accept(BaseResult<DefaultSite> defaultSiteBaseResult) throws Exception {
                        view.dismissLoading();
                        if (defaultSiteBaseResult.businessLogicSuccess()) {
                            view.showSite(defaultSiteBaseResult.getData());
                            orderEntity.setAddress(defaultSiteBaseResult.getData().getAddrId() + "");
                        } else {
                            view.showToast(defaultSiteBaseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showToast(throwable.getMessage());
                    }
                }));
    }


    @Override
    public void save() {

        if (!isChecked) {
            return;
        }

        if (TextUtils.isEmpty(orderEntity.getAddress())) {
            view.showToast("请选择收货地区");
            return;
        }

        //支付宝支付
        orderEntity.setPayType("alipayApp");

        addDisposables(ShopModel.placeOrder(orderEntity).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<String>>() {
                    @Override
                    public void accept(BaseResult<String> commoditeDetailsBaseResult) throws Exception {

                        view.dismissLoading();
                        if (commoditeDetailsBaseResult.businessLogicSuccess()) {
                            view.navPay(commoditeDetailsBaseResult.getData());
                        } else {
                            view.showToast(commoditeDetailsBaseResult.getMessage());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                    }
                }));
    }

    @Override
    public void onChecked(boolean b) {
        isChecked = b;
    }

    @Override
    public void setAddress(String address) {
        orderEntity.setAddress(address);
    }

    @Override
    public void setByType(String byType) {
        orderEntity.setByType(byType);
    }

    @Override
    public void setPackageId(String packageId) {
        orderEntity.setPackageId(packageId);
    }

    @Override
    public void setPayType(String payType) {
        orderEntity.setByType(payType);
    }

    @Override
    public void getUserInfo() {
        UserInfoModel.loadUserInfo()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(
                        new Consumer<BaseResult<UserInfo>>() {
                            @Override
                            public void accept(BaseResult<UserInfo> baseResult) throws Exception {
                                if (baseResult.businessLogicSuccess()) {
                                    UserInfoManger.INSTANCE.clear();
                                    UserInfoManger.INSTANCE.init(baseResult.getData());

                                } else {
                                    view.showToast(baseResult.getMessage());
                                }
                            }
                        },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                view.showToast(throwable.getMessage());
                            }
                        });
    }

    @Override
    public void addDisposables(Disposable disposable) {
        super.addDisposables(disposable);
        view.showLoading();
    }
}